/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jims.master.modules.doctor.netHosptial.service;

import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.RestUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.netHosptial.dao.NetExamAppointsDao;
import com.jims.master.modules.doctor.netHosptial.dao.NetExamItemsDao;
import com.jims.master.modules.doctor.netHosptial.entity.*;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 检查预约记录Service
 *
 * @author qlx
 * @version 2016-09-10
 */
@Service
@Transactional(readOnly = true)
public class NetExamAppointsService extends CrudService<NetExamAppointsDao, NetExamAppoints> {

    @Autowired
    private NetExamItemsDao hisExamItemsDao;

    @Autowired
    private NetPatMasterService netPatMasterService;

    @Autowired
    private NetClinicMasterService netClinicMasterService;


    @Autowired
    private NetOrdersService hisOutpOrdersService;



    //查询最大examNo
    public String findMaxExamAppoints() {
        return dao.findMaxExamAppoints();
    }

    //调用接口方法（itemcode表示map的value值，keyName表示map中key的名称）
    public List<BaseDto> getExamList(String url, String itemcode, String keyName) {
        List<BaseDto> patientList = new ArrayList<BaseDto>();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put(keyName, itemcode);
        params.put("itemClass", "D");
        RestUtils<BaseDto, HashMap> restUtils = new RestUtils<BaseDto, HashMap>();
        try {
            Map<String, Object> map = restUtils.findByList(url, BaseDto.class, params, params);
            patientList = (List<BaseDto>) map.get("body");
        } catch (Exception e) {
            e.getMessage();
        }
        return patientList;
    }

    /**
     * 获得患者未传送的信息
     * @param-patientId
     * @return
     */
    public List<NetExamAppoints> getInfoToSend(NetExamAppoints hisExamAppoints){
        return dao.getInfoToSend(hisExamAppoints);
    };

    //改变发送标志
    @Transactional(readOnly = false)
    public Integer send(NetExamAppoints hisExamAppoints){
        return dao.send(hisExamAppoints);
    }

    @Transactional(readOnly = false)
    public void saveVillageAll(NetExamAppoints hisExamAppoints) throws Exception{
        //1、patMasterIndex
        NetPatMaster patMasterIndex = netPatMasterService.findIndexInfoByPatientId(hisExamAppoints.getPatientId());
        //2、patientId visitDate 初始化 ClinicMaster
        NetClinicMaster clinicMaster = netClinicMasterService.get(hisExamAppoints.getClinicId());
        //3、NetExamAppoints
        hisExamAppoints.preInsert();
        //EXAM_NO ae+num a:医院序号、e申请号字段首字母
        String examNo = findMaxExamAppoints();
        if (StringUtils.isEmpty(examNo)) {
            examNo = "AE1";
        } else {
            examNo = "AE" + examNo;
        }
        hisExamAppoints.setExamNo(examNo);
        //VISIT_ID 门诊
        String visitId = "0";
        hisExamAppoints.setVisitId(visitId);
        hisExamAppoints.setPatientId(patMasterIndex.getPatientId());
        //hisExamAppoints.setOrgId("");
        //NAME, NAME_PHONETIC, SEX, BIRTH_PLACE,
        hisExamAppoints.setName(patMasterIndex.getName());
        hisExamAppoints.setNamePhonetic("");
        hisExamAppoints.setSex(patMasterIndex.getSex());
        hisExamAppoints.setBirthPlace("");
        hisExamAppoints.setDateOfBirth(patMasterIndex.getDateOfBirth());
        //charge_type , identity ,
        hisExamAppoints.setChargeType(patMasterIndex.getChargeType());
        hisExamAppoints.setIdentity(patMasterIndex.getIdentity());
        //MAILING_ADDRESS, ZIP_CODE, PHONE_NUMBER,
        hisExamAppoints.setMailingAddress(null);
        hisExamAppoints.setZipCode(null);
        hisExamAppoints.setPhoneNumber(null);
        //EXAM_CLASS, EXAM_SUB_CLASS
        //CLIN_SYMP, PHYS_SIGN, RELEVANT_LAB_TEST, RELEVANT_DIAG, CLIN_DIAG
        hisExamAppoints.setClinDiag("");
        //EXAM_MODE, EXAM_GROUP,
        hisExamAppoints.setExamMode(null);
        hisExamAppoints.setExamGroup(null);
        //PERFORMED_BY,
        //PATIENT_SOURCE, FACILITY,
        hisExamAppoints.setPatientSource("");
        hisExamAppoints.setFacility(null);
        //REQ_DEPT, REQ_PHYSICIAN,DOCTOR_USER,
        hisExamAppoints.setReqDept(clinicMaster.getVisitDept());
        hisExamAppoints.setReqDeptName(clinicMaster.getVisitDeptName());
        //todo
        //hisExamAppoints.setDoctorUser(UserUtils.getBaseInfo().getHisloginname());
        hisExamAppoints.setReqDateTime(clinicMaster.getVisitDate());
        //hisExamAppoints.setReqPhysician(UserUtils.getBaseInfo().getName());
        ///NOTICE,
        //  WARD_CODE,
        hisExamAppoints.setWardCode("");
        //PRIORITY_INDICATOR
        hisExamAppoints.setPriorityIndicator("0");
        //CNSLT_STATE
        hisExamAppoints.setCnsltState("0");
        hisExamAppoints.setRemarks("0");
        List<NetExamItems> labTestItemsList = hisExamAppoints.getItemList();
        //costs charges
        Float cost = 0f;
        for(NetExamItems entity_cost:labTestItemsList){
            cost=cost+Float.valueOf(entity_cost.getCosts());
        }
        cost = (float)(Math.round(cost*100))/100;
        hisExamAppoints.setCosts(cost.toString());
        hisExamAppoints.setCharges(cost.toString());
        //save
        dao.insert(hisExamAppoints);

        for (int i = 0; i < labTestItemsList.size(); i++) {
            //6、NetExamItems
            NetExamItems hisExamItems = labTestItemsList.get(i);
            //hisExamItems.setAppointsId(hisExamAppoints.getId());
            hisExamItems.setExamNo(examNo);
            hisExamItems.setExamItemNo(String.valueOf(i + 1));
            hisExamItems.preInsert();
            hisExamItemsDao.insert(hisExamItems);
        }
    }

    /**
     * 由挂号表关联检验
     * @param-clinicId
     * @return
     */
    public List<NetExamAppoints> getByClinicId(NetExamAppoints hisExamAppoints){
        return dao.getByClinicId(hisExamAppoints);
    }
    @Transactional(readOnly = false)
    public void deleteAll(NetExamAppoints hisExamAppoints){
        dao.delete(hisExamAppoints);
    }

    public NetExamAppoints getInfoByexamNo(NetExamAppoints hisExamAppoints){
        return dao.getInfoByexamNo(hisExamAppoints);
    }
}